﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace CentralOffice.WebClient
{
    public partial class db : System.Web.UI.Page
    {
        //private const string Select_AllDatabase = "select SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, SQL_PATH from information_schema.SCHEMATA";
        //private const string Select_Tables = "select TABLE_CATALOG, TABLE_NAME, TABLE_ROWS, DATA_LENGTH, CREATE_TIME, UPDATE_TIME, TABLE_COMMENT from information_schema.TABLES WHERE TABLE_SCHEMA = 'pcmisdb' ";
        private const string Select_Tables = "SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties' AND TABLE_CATALOG='{0}' ORDER BY TABLE_SCHEMA";
        //private const string Select_Columns = "select TABLE_CATALOG,  TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, COLUMN_TYPE, COLUMN_KEY, COLUMN_COMMENT from  information_schema.COLUMNS  WHERE TABLE_SCHEMA = 'pcmisdb' ";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                try
                {
                    GridView1.DataSource = ExecuteDataTable(string.Format(Select_Tables, System.Configuration.ConfigurationManager.AppSettings["DatabaseName"]));
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }


        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text.Length == 0) return;
                if (TextBox1.Text.Trim().ToLower().StartsWith("select") || TextBox1.Text.Trim().ToLower().StartsWith("exec"))
                {
                    GridView2.DataSource = ExecuteDataTable(TextBox1.Text);
                    GridView2.DataBind();
                }
                else
                {
                    lblError.Text = "影响的记录数为： " + ExecuteNoQuery(TextBox1.Text);
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        protected void btnBackup_Click(object sender, EventArgs e)
        {
            
            //cn.ConnectionString.

            System.Configuration.ConnectionStringSettings conectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CentralOfficeConnectionString"];
            string[] conectionStringSegments = conectionString.ConnectionString.Split(';');
            string databaseName = conectionStringSegments[1].Split('=')[1];

            string downloadPath = SiteUtil.GetDocumentFilePath()  + DateTime.Now.ToString("yyyyMMdd") + ".bak";

            string sql = "BACKUP DATABASE [" + databaseName + "] to disk='" + Server.MapPath( downloadPath) + "' with format";

            ExecuteNoQuery(sql);

            SiteUtil.GenernateDownloadHyperLink(linkDownload, downloadPath);
             
        }

        private static DataTable ExecuteDataTable(string sql)
        {
            DataTable dt = null;
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(sql);
                DataSet ds = db.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch
            {
                throw;
            }
            finally
            {

            }
            return dt;
        }

        private static int ExecuteNoQuery(string sql)
        {
            int cnt = 0;
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(sql);
                cnt = db.ExecuteNonQuery(cmd);
            }
            catch
            {
                throw;
            }
            finally
            {
            }
            return cnt;
        }
    }
}
